package database

import (
	"time"

	"github.com/dheisomgomes/codeapi/database/migrations"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

var db *gorm.DB

func StartDatabase(f string) error {
	database, err := gorm.Open(sqlite.Open(f), &gorm.Config{})
	if err != nil {
		return err
	}
	db = database
	config, _ := db.DB()
	config.SetMaxIdleConns(10)
	config.SetMaxOpenConns(100)
	config.SetConnMaxLifetime(time.Hour)
	migrations.RunMigrations(db)
	return nil
}

func GetDatabase() *gorm.DB {
	return db
}
